package command.project.statechart;

import javax.xml.bind.annotation.XmlTransient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import controller.ApplicationController;
import model.Statechart;
import model.User;

public class CreateStatechartCommand extends AbstractStatechartCommand {

	private Long projectID;
	private String statechartName;
	private String userName;
	private Statechart statechart;

	@XmlTransient
	private static final Logger log = LoggerFactory
			.getLogger(CreateStatechartCommand.class);

	public CreateStatechartCommand(String userName, Long projectID,
			String statechartName) {
		super();
		this.userName = userName;
		this.projectID = projectID;
		this.statechartName = statechartName;
	}

	@Override
	public void executeLocal(ApplicationController applicationController) {
		log.debug("Executing command : CreateStatechartCommand ...");
		User user = applicationController.getUserByName(userName);
		statechart = applicationController.getStatechartFactory()
				.createStatechart(statechartName, user);
		log.debug("Executing command : statechart created, adding to current project");
		applicationController.getModel().getCurrentProject()
				.addStatechart(statechart);
		log.debug("Executing command : CreateStatechartCommand DONE");
	}

	@Override
	public void executeRemote(ApplicationController applicationController) {
		log.debug("Executing remote command : CreateStatechartCommand (add statechart to current project)...");
		if (applicationController.getModel().getCurrentProject() != null
				&& applicationController.getModel().getCurrentProject().getId()
						.equals(projectID)) {
			log.debug("Executing remote command : CreateStatechartCommand (projet is open) ...");
			applicationController.getModel().getCurrentProject()
					.addStatechart(statechart);
		} else {
			log.debug("Executing remote command : CreateStatechartCommand (projet is not open) ...");
		}
		log.debug("Executing remote command : CreateStatechartCommand DONE");
	}

	public Statechart getStatechart() {
		return statechart;
	}

}
